"""
General constant string used in VN Trader.
"""

from enum import Enum


class Direction(Enum):
    """
    Direction of order/trade/position.
    """
    LONG = "多"
    SHORT = "空"
    NET = "净"


class DirectType(Enum):
    """买卖"""
    Buy = 0
    """买"""
    Sell = 1
    """卖"""


class OffsetType(Enum):
    """开平"""
    Open = 0
    '''开'''
    Close = 1
    '''平'''


class Offset(Enum):
    """
    Offset of order/trade.
    """
    NONE = ""
    OPEN = "开"
    CLOSE = "平"
    CLOSETODAY = "平今"
    CLOSEYESTERDAY = "平昨"


class Status(Enum):
    """
    Order status.
    """
    SUBMITTING = "提交中"
    NOTTRADED = "未成交"
    PARTTRADED = "部分成交"
    ALLTRADED = "全部成交"
    CANCELLED = "已撤销"
    REJECTED = "拒单"


class Product(Enum):
    """
    Product class.
    """
    EQUITY = "股票"
    FUTURES = "期货"
    OPTION = "期权"
    INDEX = "指数"
    FOREX = "外汇"
    SPOT = "现货"
    ETF = "ETF"
    BOND = "债券"
    WARRANT = "权证"
    SPREAD = "价差"
    FUND = "基金"


class OrderPriceType(Enum):
    """
    Order type.
    """
    LIMIT = "限价"
    MARKET = "市价"
    STOP = "STOP"
    FAK = "FAK"
    FOK = "FOK"


class OrderType(Enum):
    BUY = '开多'
    SELL = '平多'
    SHORT = '开空'
    COVER = '平空'


class OptionType(Enum):
    """
    Option type.
    """
    CALL = "看涨期权"
    PUT = "看跌期权"


class Exchange(Enum):
    """
    Exchange.
    """
    # Chinese
    CFFEX = "CFFEX"  # China Financial Futures Exchange
    SHFE = "SHFE"  # Shanghai Futures Exchange
    CZCE = "CZCE"  # Zhengzhou Commodity Exchange
    DCE = "DCE"  # Dalian Commodity Exchange
    INE = "INE"  # Shanghai International Energy Exchange
    SSE = "SSE"  # Shanghai Stock Exchange
    SZSE = "SZSE"  # Shenzhen Stock Exchange
    SGE = "SGE"  # Shanghai Gold Exchange
    WXE = "WXE"  # Wuxi Steel Exchange

    # Global
    SMART = "SMART"  # Smart Router for US stocks
    NYMEX = "NYMEX"  # New York Mercantile Exchange
    COMEX = "COMEX"  # a division of theNew York Mercantile Exchange
    GLOBEX = "GLOBEX"  # Globex of CME
    IDEALPRO = "IDEALPRO"  # Forex ECN of Interactive Brokers
    CME = "CME"  # Chicago Mercantile Exchange
    ICE = "ICE"  # Intercontinental Exchange
    SEHK = "SEHK"  # Stock Exchange of Hong Kong
    HKFE = "HKFE"  # Hong Kong Futures Exchange
    SGX = "SGX"  # Singapore Global Exchange
    CBOT = "CBT"  # Chicago Board of Trade
    CBOE = "CBOE"  # Chicago Board Options Exchange
    CFE = "CFE"  # CBOE Futures Exchange
    DME = "DME"  # Dubai Mercantile Exchange
    EUREX = "EUX"  # Eurex Exchange
    APEX = "APEX"  # Asia Pacific Exchange
    LME = "LME"  # London Metal Exchange
    BMD = "BMD"  # Bursa Malaysia Derivatives
    TOCOM = "TOCOM"  # Tokyo Commodity Exchange
    EUNX = "EUNX"  # Euronext Exchange
    KRX = "KRX"  # Korean Exchange

    OANDA = "OANDA"  # oanda.com

    # CryptoCurrency
    BITMEX = "BITMEX"
    OKEX = "OKEX"
    HUOBI = "HUOBI"
    BITFINEX = "BITFINEX"
    BINANCE = "BINANCE"
    BYBIT = "BYBIT"  # bybit.com
    COINBASE = "COINBASE"
    GATEIO = "GATEIO"
    BITSTAMP = "BITSTAMP"

    # Special Function
    LOCAL = "LOCAL"  # For local generated data


class Currency(Enum):
    """
    Currency.
    """
    USD = "USD"
    HKD = "HKD"
    CNY = "CNY"


class Interval(Enum):
    """
    Interval of bar data.
    """
    TICK = 't'
    MINUTE = "1m"
    HOUR = "1h"
    DAILY = "d"
    WEEKLY = "w"
    MONTH = 'm'
    QUARTER = 'q'
    YEAR = 'y'


class IntervalType:
    """时间间隔（周期）类型:tick, 秒,分,时,日,周,月,年"""
    Tick = 0

    Second = 1
    '''秒'''

    Minute = 1 * 60
    '''分'''

    Hour = Minute * 60
    '''时'''

    Day = Hour * 24
    '''日'''

    Week = Day * 7
    '''周'''

    Month = Day * 30
    '''月'''

    Year = Day * 365
    '''年'''

    @classmethod
    def to_type_int(cls, period_sec: int):
        if period_sec % cls.Year == 0:
            return cls.Year
        elif period_sec % cls.Month == 0:
            return cls.Month
        elif period_sec % cls.Week == 0:
            return cls.Week
        elif period_sec % cls.Day == 0:
            return cls.Day
        elif period_sec % cls.Hour == 0:
            return cls.Hour
        elif period_sec % cls.Minute == 0:
            return cls.Minute
        elif period_sec % cls.Second == 0:
            return cls.Second
        else:
            return cls.Tick

    @classmethod
    def to_interval(cls, period_sec: int) -> Interval:
        nm = cls.to_type_int(period_sec)
        if nm == cls.Year:
            return Interval.YEAR
        elif nm == cls.Month:
            return Interval.MONTH
        elif nm == cls.Week:
            return Interval.WEEKLY
        elif nm == cls.Day:
            return Interval.DAILY
        elif nm == cls.Hour:
            return Interval.HOUR
        elif nm == cls.Minute:
            return Interval.MINUTE
        else:
            return Interval.TICK


period_second_dict = {
    't': 0,
    '1m': 60,
    '3m': 180,
    '5m': 300,
    '10m': 600,
    '15m': 900,
    '30m': 1800,
    '1h': 3600,
    '1d': 86400,
    '1w': 604800,
    'M': 18144000,
    'hy': 108864000,
    'y': 217728000,

}


class Period:
    """周期的定义
    interval_type: IntervalType.Tick，以秒计的周期的时间单位
    interval: Interval.TICK，周期的时间单位
    intervals: int = 1，以秒计的周期间隔，0＝＝Tick
    """
    interval: Interval.TICK = Interval.MINUTE
    interval_type: IntervalType.Tick = IntervalType.Minute
    intervals: int = 60

    @property
    def periods(self):
        """以 interval 计的周期数
        当 self.interval == IntervalType.Tick or self.intervals < self.interval
        时，返回 self.interval ,否则，返回 int(self.intervals / self.interval)
        """
        return self.intervals if self.interval == IntervalType.Tick or self.intervals < self.interval_type else int(
            self.intervals / self.interval_type)


# 策略或指标用周期类型
PERIOD_TYPE_TICK = 0
PERIOD_TYPE_M1 = 1 * 60
PERIOD_TYPE_M3 = 3 * 60
PERIOD_TYPE_M5 = 5 * 60
PERIOD_TYPE_M10 = 10 * 60
PERIOD_TYPE_M15 = 15 * 60
PERIOD_TYPE_M30 = 30 * 60
PERIOD_TYPE_H1 = 60 * 60
PERIOD_TYPE_DAY = 60 * 60 * 24
PERIOD_TYPE_WEEK = 7 * 24 * 60 * 60
PERIOD_TYPE_MONTH = 60 * 60 * 24 * 30
PERIOD_TYPE_YEAR = 60 * 60 * 24 * 365

period_en_zh_dict = {
    "TICK": "分时",
    "M1": "1分钟",
    "M3": "3分钟",
    "M5": "5分钟",
    "M10": "10分钟",
    "M15": "15分钟",
    "M30": "30分钟",
    "H1": "1小时",
    "DAY": "日",
    "WEEK": "周",
    "MONTH": "月",
    "YEAR": "年"
}
period_zh_en_dict = {v: k for k, v in period_en_zh_dict.items()}
period_en_value_dict = {
    "TICK": PERIOD_TYPE_TICK,
    "M1": PERIOD_TYPE_M1,
    "M3": PERIOD_TYPE_M3,
    "M5": PERIOD_TYPE_M5,
    "M10": PERIOD_TYPE_M10,
    "M15": PERIOD_TYPE_M15,
    "M30": PERIOD_TYPE_M30,
    "H1": PERIOD_TYPE_H1,
    "DAY": PERIOD_TYPE_DAY,
    "WEEK": PERIOD_TYPE_WEEK,
    "MONTH": PERIOD_TYPE_MONTH,
    "YEAR": PERIOD_TYPE_YEAR
}
period_zh_value_dict = {k: period_en_value_dict[v] for k, v in period_zh_en_dict.items()}

future_exchange_dict = {'CFFEX': '中金所', 'DCE': '大期所', 'CZCE': '郑期所', 'SHFE': '上期所', 'INE': '能源所',
                        'SSFuture': '自选', 'MainFuture': '主力', 'TradingFuture': '交易', }
stock_exchange_dict = {'SSStock': '自选股票', 'SZSE': '深交所', 'SSE': '上交所'}
